package com.aliware.tianchi;

import org.apache.dubbo.rpc.listener.CallbackListener;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author lorne
 * @date 2019/6/25
 * @description
 */
public class ProviderContext {

    private static ProviderContext context;

    private ProviderContext(){
        listeners = new ConcurrentHashMap<>();
    }

    public static ProviderContext getInstance(){
        if (context == null){
            synchronized (ProviderContext.class){
                if(context==null){
                    context = new ProviderContext();
                }
            }
        }
        return context;
    }



    private String threads;

    private Map<String,CallbackListener> listeners;

    public String getThreads() {
        return threads;
    }

    public void setThreads(String threads) {
        this.threads = threads;
    }


    public void sendThreads(String key){
        if(threads!=null) {
            for (Map.Entry<String, CallbackListener> entry : listeners.entrySet()) {
                try {
                    entry.getValue().receiveServerMsg(key+"#"+threads);
                } catch (Throwable t1) {
                    listeners.remove(entry.getKey());
                }
            }
        }
    }

    public void put(String key, CallbackListener listener) {
        listeners.put(key, listener);
    }
}
